package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.SmsHomeNewProductMapper;
import com.itheima.pojo.SmsHomeNewProduct;
import com.itheima.pojo.SmsHomeRecommendSubject;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@DubboService
public class SmsHomeNewProductImpl implements SmsHomeNewProductApi {

    @Autowired
    private SmsHomeNewProductMapper mapper;

    /**
     * 添加首页新品
     */
    @Override
    public boolean create(List<SmsHomeNewProduct> newProductList) {

        boolean flag = false;
        LambdaQueryWrapper<SmsHomeNewProduct> lqw = new LambdaQueryWrapper<>();
        lqw.ne(SmsHomeNewProduct::getId, 0);
        List<SmsHomeNewProduct> smsHomeNewProducts = mapper.selectList(lqw);

        for (SmsHomeNewProduct smsHomeNewProduct : newProductList) {
            for (SmsHomeNewProduct homeNewProduct : smsHomeNewProducts) {
                if (StringUtils.pathEquals(homeNewProduct.getProductName(), smsHomeNewProduct.getProductName())) {
                    flag = true;
                    return flag;
                }
            }
            mapper.insert(smsHomeNewProduct);
        }
        return flag;
    }

    /**
     * 批量删除首页新品
     */
    @Override
    public void delete(List<Integer> ids) {
        mapper.deleteBatchIds(ids);
    }


    /**
     * 批量修改首页新品状态
     */
    @Override
    public void updateStatus(Integer recommendStatus, List<Integer> ids) {
        List<SmsHomeNewProduct> smsHomeNewProducts = mapper.selectBatchIds(ids);
        for (SmsHomeNewProduct smsHomeNewProduct : smsHomeNewProducts) {
            smsHomeNewProduct.setRecommendStatus(recommendStatus);
            mapper.updateById(smsHomeNewProduct);
        }
    }

    /**
     * 修改首页新品排序
     */
    @Override
    public void updateSort(Integer id, Integer sort) {
        SmsHomeNewProduct smsHomeNewProduct = mapper.selectById(id);
        smsHomeNewProduct.setSort(sort);
        mapper.updateById(smsHomeNewProduct);
    }

    /**
     * 首页新品分页查询
     */
    @Override
    public IPage<SmsHomeNewProduct> list(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {

        IPage<SmsHomeNewProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeNewProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(recommendStatus), SmsHomeNewProduct::getRecommendStatus, recommendStatus)
                .like(!ObjectUtils.isEmpty(productName), SmsHomeNewProduct::getProductName, productName)
                .orderByDesc(SmsHomeNewProduct::getSort);

        mapper.selectPage(page, wrapper);

        return page;
    }


}
